namespace ZerAx.Net
{
    public class InterThreadConnection : IConnection
    {
        private readonly Channel<Packet>.Sender toClient;
        private readonly Channel<Packet>.Receiver fromClient;

        public InterThreadConnection(Channel<Packet>.Sender toClient, Channel<Packet>.Receiver fromClient)
        {
            this.toClient = toClient;
            this.fromClient = fromClient;
        }

        public static (InterThreadConnection, InterThreadConnection) CreatePair()
        {
            var (csend, srecv) = Channel<Packet>.Create();
            var (ssend, crecv) = Channel<Packet>.Create();
            return (new InterThreadConnection(csend, crecv), new InterThreadConnection(ssend, srecv));
        }

        public bool IsConnecting => !toClient.IsClosed && !fromClient.IsClosed;

        public void Disconnect()
        {
            fromClient.Close();
        }

        public bool Poll(out Packet packet)
        {
            return fromClient.Poll(out packet);
        }

        public void Send(Packet packet)
        {
            toClient.Send(packet);
        }

        public void Update()
        {

        }
    }
}